import {messages} from "share/common";
import React, { Component } from "react"
import MulModal from "components/mul-modal"
import DepartmentTransfer from "components/template/department/department-transfer"
import ChooserContent from "components/mul-modal/chooser-content"
import ChooserSelect from "components/mul-modal/chooser-select"
import ChooserHoc from "components/template/department/chooser-hoc"

@ChooserHoc
class DepartmentDialog extends Component {

  searchFocus = index => {
    this.modalRef.changeIndex(index);
  };

  getExtraSearch = direction => {
    const { showCompany, companyValue, setSource } = this.props;
    if (showCompany && direction === 'left')
      return (
        <div style={{ width: '148px', flex: 'none', marginRight: '10px' }}>
          <ChooserSelect
            placeholder={messages('common-1.key287')/*选择公司*/}
            value={companyValue}
            handleFocus={() => this.searchFocus(1)}
            clear={() => setSource([], 'companyValue')}
          />
        </div>
      );
    return null
  };

  render() {
    const { visible, value, onCancel, onOk, needEntity, isNeedCompany, setSource, selectedValue, parser,
      hideSubDepartment, single, zIndex } = this.props;
    return (
      <MulModal
        title={messages('common-1.key288')/*选择部门*/}
        visible={visible}
        destroyOnClose
        onCancel={() => {
          setSource([], 'companyValue');
          onCancel()
        }}
        onRef={modalRef => this.modalRef = modalRef}
        zIndex={zIndex || 1000}
        content={[
          <DepartmentTransfer
            onRef={departmentRef => this.departmentRef = departmentRef}
            value={value}
            companyId={selectedValue[0] ? selectedValue[0].id : ''}
            needEntity={needEntity}
            parser={parser}
            isNeedCompany={isNeedCompany}
            getExtraSearch={this.getExtraSearch}
            hideSubDepartment={hideSubDepartment}
            single={single}
          />,
          <ChooserContent
            type={'available_company_setOfBooks'}
            labelKey={'name'}
            valueKey={'id'}
            single
            value={selectedValue}
            onRef={companyRef => this.companyRef = companyRef}
            listExtraParams={{enabled: true}}
          />
        ]}
        onOkArray={[
          () => {
            setSource([], 'companyValue');
            onOk(this.departmentRef.handleOk())
          },
          () => {
            this.searchFocus(0);
            setSource(this.companyRef.handleOk(), 'selectedValue');
          }
        ]}
      />
    )
  }

}

export default DepartmentDialog
